package com.moody.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moody.hibernate.domain.ClassifyRecord;
import com.moody.hibernate.domain.CollateralInfo;
import com.moody.hibernate.domain.CustDefaultEvent;
import com.moody.hibernate.domain.CustInfo;
import com.moody.hibernate.domain.DebtCollateral;
import com.moody.hibernate.domain.DebtCollateralId;
import com.moody.hibernate.domain.DebtInfo;
import com.moody.hibernate.domain.GuaranteeInfo;
import com.moody.hibernate.domain.GuaranteeInfoId;
import com.moody.hibernate.domain.GuarantorInfo;
import com.moody.hibernate.domain.UserInfo;
import com.moody.init.Collateral;
import com.moody.init.DataDictionary;
import com.moody.init.Debt;
import com.moody.init.Guaranty;
import com.moody.init.Industry;
import com.moody.service.CustomerService;
import com.moody.service.ObligationService;
import com.moody.util.CalendarUtil;

/*
 * Copyright © Moody analytics   
 *
 * @Title: ObligationController.java 
 * @Package： com.moody.web 
 * @author： Fuqiang Wang
 * @Description: 债项管理：核心功能
 * @date： 2012-3-16 上午10:42:32
 * @Modified by: Fuqiang Wang 
 * @version V1.0   
 */

@Controller
@RequestMapping("/obligation")
public class ObligationController {
	private static final Logger log = LoggerFactory
	.getLogger(FinancialAnalysisController.class);

	@Resource
	private ObligationService obligationService;
	@Resource
	private CustomerService customerService;
	
	//抵质押品列表
	@RequestMapping("/listCollateral")
	public void listCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();
		 Integer start = Integer.parseInt(request.getParameter("start"));
		 Integer limit = Integer.parseInt(request.getParameter("limit"));		 
		 
		 List<CollateralInfo> myCollateralList = new ArrayList<CollateralInfo>();
		 myCollateralList = obligationService.findAllCollateralByInputID(); 
		 
		 List<CollateralInfo> pageCollateralList;


		// 分页,该页size未超过剩余size
		 if (myCollateralList.size() > start + limit)
			pageCollateralList = myCollateralList.subList(start, limit + start);
		 else
			pageCollateralList = myCollateralList.subList(start, myCollateralList.size());
		 
		 for(CollateralInfo ci: pageCollateralList){
			 ci.setCollateralType(DataDictionary.getInstance().getNameByCodeAndId("GuarantyType", ci.getCollateralType()));
		 }

		 Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		 myCustMap.put("total", myCollateralList.size());
		 myCustMap.put("collateral", pageCollateralList);
		 JSONObject myCustJO = JSONObject.fromObject(myCustMap);
		 out.print(myCustJO);
	}
	
	//查询客户
	@RequestMapping("/findCollateral")
	public void findCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{	
		String conditionKey = request.getParameter("condition");
		String conditionValue = request.getParameter("keyword");
		List<CollateralInfo> list = new ArrayList<CollateralInfo>(); 
		Map<String,String> condition = new LinkedHashMap<String,String>();
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
			
		//if(conditionKey.equals("custId")) conditionKey = "custNo";//转换成按照客户号查询
		condition.put(conditionKey, conditionValue);
		list = obligationService.findCollateralByCondition(condition, userId, orgId);
			
		//分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
			
		List<CollateralInfo> pageList;
		if(list.size()>start+limit)
			pageList = list.subList(start, limit+start); 
		else
			pageList = list.subList(start,list.size());
		
		 for(CollateralInfo ci: pageList){
			 ci.setCollateralType(DataDictionary.getInstance().getNameByCodeAndId("GuarantyType", ci.getCollateralType()));
		 }
			
		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("success", "true");
		myCustMap.put("total", list.size());
		myCustMap.put("resultData", pageList);
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
			
		log.info("JSON data: "+myCustJO);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(myCustJO);
			
		}	

	//保证人列表
	@RequestMapping("/listGuarantor")
	public void listGuarantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();
		 
		 Integer start = Integer.parseInt(request.getParameter("start"));
		 Integer limit = Integer.parseInt(request.getParameter("limit"));	
		 
		 List<GuarantorInfo> pageGuarantorList;
		 
		 List<GuarantorInfo> myGuarantoryList = new ArrayList<GuarantorInfo>();
		 myGuarantoryList = obligationService.findAllGuarantor(); 
		 
			// 分页,该页size未超过剩余size
		 if (myGuarantoryList.size() > start + limit)
			 pageGuarantorList = myGuarantoryList.subList(start, limit + start);
		 else
			 pageGuarantorList = myGuarantoryList.subList(start, myGuarantoryList.size());
		 
		 for(GuarantorInfo ci: pageGuarantorList){
			 ci.setGuarantorType(DataDictionary.getInstance().getNameByCodeAndId("GuarantorType",ci.getGuarantorType()));
			 ci.setIsOurCustomer(DataDictionary.getInstance().getNameByCodeAndId("YesNo",ci.getIsOurCustomer()));
		 }

		 Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		 myCustMap.put("total", myGuarantoryList.size());
		 myCustMap.put("guarantory", pageGuarantorList);
		 JSONObject myCustJO = JSONObject.fromObject(myCustMap);
		 out.print(myCustJO);
	}	
	
	//查询客户
	@RequestMapping("/findGuarantor")
	public void findGuarantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{	
		String conditionKey = request.getParameter("condition");
		String conditionValue = request.getParameter("keyword");
		List<GuarantorInfo> list = new ArrayList<GuarantorInfo>(); 
		Map<String,String> condition = new LinkedHashMap<String,String>();
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
			
		//if(conditionKey.equals("custId")) conditionKey = "custNo";//转换成按照客户号查询
		condition.put(conditionKey, conditionValue);
		list = obligationService.findGuarantorByCondition(condition, userId, orgId);
			
		//分页
		Integer start = Integer.parseInt(request.getParameter("start"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
			
		List<GuarantorInfo> pageList;
		if(list.size()>start+limit)
			pageList = list.subList(start, limit+start); 
		else
			pageList = list.subList(start,list.size());
		
		 for(GuarantorInfo ci: pageList){
			 ci.setGuarantorType(DataDictionary.getInstance().getNameByCodeAndId("GuarantorType",ci.getGuarantorType()));
			 ci.setIsOurCustomer(DataDictionary.getInstance().getNameByCodeAndId("YesNo",ci.getIsOurCustomer()));
		 }
			
		Map<String, Object> myCustMap = new HashMap<String, Object>(3);
		myCustMap.put("success", "true");
		myCustMap.put("total", list.size());
		myCustMap.put("resultData", pageList);
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
			
		log.info("JSON data: "+myCustJO);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(myCustJO);
			
	}
	
	@RequestMapping("/listCurrentCollateral")
	public void listCurrentCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 List<CollateralInfo> myCollateralList = new ArrayList<CollateralInfo>();
		 
		 myCollateralList = obligationService.findAllCollateralByInputID(); 
		 
		 List resultList = new ArrayList();
		 
		 for(CollateralInfo ci: myCollateralList){
			 Map map = new HashMap();
			 map.put("code", ci.getCollateralId());
			 map.put("name", ci.getCollateralName());
			 resultList.add(map);
		 }

		 JSONArray JA=JSONArray.fromObject(resultList);
		 out.print(JA);
	}
	
	//新增抵质押品
	@RequestMapping("/addCollateral")
	public void addCollateral(HttpServletRequest request,
			HttpServletResponse response, CollateralInfo collateralInfo) throws IOException{
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		collateralInfo.setInputUserId(user.getUserId());
		collateralInfo.setInputOrgId(user.getBelongorg());
		obligationService.insertCollateral(collateralInfo);
		response.getWriter().print("{success:true}");
	}
	
	//获取单一抵质押品信息
	@RequestMapping("/getCollateral")
	public void getCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();
		 
		 String collateralId = request.getParameter("collateralId");

		 CollateralInfo collateral = new CollateralInfo();
		 collateral = obligationService.findCollateralById(collateralId); 
		 
		 
		 String levelOneCode = collateral.getCollateralType().substring(0, 4);
		 collateral.setCollateralTypeLevelOneCode(levelOneCode);
		 List list = Collateral.getInstance().getFirstLevel();
		 Iterator it = list.iterator();
		 while(it.hasNext()){
			 Map itemMap = (Map)it.next();
			 if(itemMap.get("code").equals(levelOneCode))
				 collateral.setCollateralTypeLevelOneName((String)itemMap.get("name"));
		 }
		 list = (List)Collateral.getInstance().getSecondLevel().get(levelOneCode);
		 it = list.iterator();
		 while(it.hasNext()){
			 Map itemMap = (Map) it.next();
			 if(itemMap.get("code").equals(collateral.getCollateralType()))
				 collateral.setCollateralTypeLevelTwoName((String)itemMap.get("name"));
		 }
		 if(collateral.getBelongFlag()!= null && !collateral.getBelongFlag().equals("")){
			 collateral.setBelongFlag(DataDictionary.getInstance().getNameByCodeAndId("YesNo", collateral.getBelongFlag()));
			
		 }
		 JSONObject JO=JSONObject.fromObject(collateral);
		 out.print(JO);
	}

	//获取单一保证人
	@RequestMapping("/getGuarantor")
	public void getGuarantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();
		 
		 String guarantorId = request.getParameter("guarantorId");

		 GuarantorInfo guarantor = new GuarantorInfo();
		 guarantor = obligationService.findGuarantorById(guarantorId); 

		 JSONObject JO=JSONObject.fromObject(guarantor);
		 out.print(JO);
		// log.info("JSON Data: "+JO);
	}	
	

	//获取单一抵质押品信息
	@RequestMapping("/getDebtCollateral")
	public void getDebtCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 String collateralId = request.getParameter("collateralId");
		 String debtNo = request.getParameter("debtNo");

		 CollateralInfo collateral = new CollateralInfo();
		 collateral = obligationService.findCollateralById(collateralId); 
		 
		 DebtCollateralId id = new DebtCollateralId(debtNo, collateralId);
		 DebtCollateral debtCollateral = obligationService.findDebtCollateralById(id);
		 
		 collateral.setShare("" + debtCollateral.getShare());
		 
		 String levelOneCode = collateral.getCollateralType().substring(0, 4);
		 collateral.setCollateralTypeLevelOneCode(levelOneCode);
		 List list = Collateral.getInstance().getFirstLevel();
		 Iterator it = list.iterator();
		 while(it.hasNext()){
			 Map itemMap = (Map)it.next();
			 if(itemMap.get("code").equals(levelOneCode))
				 collateral.setCollateralTypeLevelOneName((String)itemMap.get("name"));
		 }
		 list = (List)Collateral.getInstance().getSecondLevel().get(levelOneCode);
		 it = list.iterator();
		 while(it.hasNext()){
			 Map itemMap = (Map) it.next();
			 if(itemMap.get("code").equals(collateral.getCollateralType()))
				 collateral.setCollateralTypeLevelTwoName((String)itemMap.get("name"));
		 }

		 JSONObject JO=JSONObject.fromObject(collateral);
		 out.print(JO);
	}
	
	//编辑抵质押品
	@RequestMapping("/editCollateral")
	public void editCollateral(HttpServletRequest request,
			HttpServletResponse response, CollateralInfo collateralInfo) throws IOException{
		
		if(collateralInfo.getBelongFlag()!= null && !collateralInfo.getBelongFlag().equals("")){
			 if(collateralInfo.getBelongFlag().equals("是"))
				 collateralInfo.setBelongFlag("1");
			 else if(collateralInfo.getBelongFlag().equals("否"))
				 collateralInfo.setBelongFlag("0");
		 }
		if(!collateralInfo.getCollateralType().substring(0, 1).equals("0")){//非Id时,collateralType中存放的为name
			collateralInfo.setCollateralType(DataDictionary.getInstance().getIdNoByCodeAndName("GuarantyType", collateralInfo.getCollateralType()));
		}
		obligationService.updateCollateral(collateralInfo);
		response.getWriter().print("{success:true}");
	}
	
	//编辑保证人
	@RequestMapping("/editGuarantor")
	public void editGuarantor(HttpServletRequest request,
			HttpServletResponse response, GuarantorInfo guarantorInfo) throws IOException{
	
		obligationService.updateGurantor(guarantorInfo);
		response.getWriter().print("{success:true}");
	}	
	
	//删除一项抵质押品
	@RequestMapping("/deleteCollateral")
	public void deleteCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String collateralId = request.getParameter("collateralId"); 
		List<DebtCollateral> debtCollateralList = obligationService.findDebtCollateralByCollateralId(collateralId);
		
		if(debtCollateralList.size() == 0){
			obligationService.deleteCollateralById(collateralId);
			response.getWriter().print("{success:true, del:true}");
		}
		else if(debtCollateralList.size() != 0){
			response.getWriter().print("{success:true, del:false}");
		}
	}
	
	//删除一项抵质押品
	@RequestMapping("/deleteGuarantor")
	public void deleteGuarantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String guarantorId = request.getParameter("guarantorId"); 
		List<GuaranteeInfo> debtGuaranteeList = obligationService.findGuaranteeByGuarantorId(guarantorId);
		
		if(debtGuaranteeList.size() == 0){
			obligationService.deleteGuarantorById(guarantorId);
			response.getWriter().print("{success:true, del:true}");
		}
		else if(debtGuaranteeList.size() != 0){
			response.getWriter().print("{success:true, del:false}");
		}
	}	

	
	//资产分类认定-已认定列表
	@RequestMapping("/listClassifyRecordConfirmed")
	public void listClassifyRecordConfirmed(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 List<ClassifyRecord> classifyRecordConfirmedList = new ArrayList<ClassifyRecord>();
		 UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		 String userId = user.getUserId();
		 String orgId = user.getBelongorg();
		 classifyRecordConfirmedList = obligationService.findAllClassifyRecordByAuthID("04",userId,orgId); 
		 
		 List resultList = new ArrayList();
		 
		 for(ClassifyRecord cr: classifyRecordConfirmedList ){
			 DebtInfo debt = new DebtInfo();
			 
			 debt = obligationService.findDebtInfoById(cr.getDebtNo());
			 if(debt == null) continue;
			 debt.setCurrency(DataDictionary.getInstance().getNameByCodeAndId("Currency", debt.getCurrency()));
			 debt.setClassifyResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", debt.getClassifyResult()));
			 debt.setDebtType(DataDictionary.getInstance().getNameByCodeAndId("DebtType", debt.getDebtType()));
			 debt.setCustName(customerService.getCustNameById(debt.getCustId()));
			 cr.setFirstResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getFirstResult()));
			 cr.setConfirmResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getConfirmResult()));
			 
			 Map map = new HashMap();
			 map.put("classify", cr);
			 map.put("debt", debt);
			 
			 resultList.add(map);
		 }

		 JSONArray JA=JSONArray.fromObject(resultList);
		 log.info("JSON: "+JA);
		 out.print(JA);
	}
	
	//查询分类认定
	@RequestMapping("/findClassifyRecord")
	public void findClassifyRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();
		 
		 String debtNo = request.getParameter("debtNo");

		 List<ClassifyRecord> classifyRecordConfirmedList = new ArrayList<ClassifyRecord>();
		 UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		 String userId = user.getUserId();
		 String orgId = user.getBelongorg();
		 classifyRecordConfirmedList = obligationService.findAllClassifyRecordByAuthID("02",userId,orgId); 
		 
		 List resultList = new ArrayList();
		 
		 for(ClassifyRecord cr: classifyRecordConfirmedList ){
			 DebtInfo debt = new DebtInfo();
			 debt = obligationService.findDebtInfoById(cr.getDebtNo());
			 
			 debt.setCurrency(DataDictionary.getInstance().getNameByCodeAndId("Currency", debt.getCurrency()));
			 
			 cr.setFirstResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getFirstResult()));
			
			 Map map = new HashMap();
			 map.put("classify", cr);
			 map.put("debt", debt);
			 
			 resultList.add(map);
		 }

		 JSONArray JA=JSONArray.fromObject(resultList);
		 out.print(JA);
	}
	
	//资产分类认定-待认定列表
	@RequestMapping("/listClassifyRecordToConfirm")
	public void listClassifyRecordToConfirm(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 List<ClassifyRecord> classifyRecordConfirmedList = new ArrayList<ClassifyRecord>();
		 
		 UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		 String userId = user.getUserId();
		 String orgId = user.getBelongorg();
		 classifyRecordConfirmedList = obligationService.findAllClassifyRecordByAuthID("02",userId,orgId); 
		 
		 List resultList = new ArrayList();
		 
		 for(ClassifyRecord cr: classifyRecordConfirmedList ){
			 DebtInfo debt = new DebtInfo();
			 debt = obligationService.findDebtInfoById(cr.getDebtNo());
			 
			 debt.setCurrency(DataDictionary.getInstance().getNameByCodeAndId("Currency", debt.getCurrency()));
			 
			 cr.setFirstResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getFirstResult()));
			
			 Map map = new HashMap();
			 map.put("classify", cr);
			 map.put("debt", debt);
			 
			 resultList.add(map);
		 }

		 JSONArray JA=JSONArray.fromObject(resultList);
		 out.print(JA);
	}
	
	//调整初分结果
	@RequestMapping("/adjustFirstResult")
	public void adjustFirstResult(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		//当前逻辑：在提交的初分信息上直接进行调整修改
		String serialNo = request.getParameter("serialNo");
		String confirmResult = request.getParameter("confirmResult");
		String confirmDesc = request.getParameter("confirmDesc");
		
		ClassifyRecord cr = new ClassifyRecord();
		cr = obligationService.findClassifyRecordById(serialNo);
		cr.setConfirmResult(confirmResult);
		cr.setConfirmDesc(confirmDesc);
//		cr.setFirstDate(CalendarUtil.getCurrentDateStr());
//		cr.setStatus("01"); 不修改状态
		
		DebtInfo debt = new DebtInfo();
		debt = obligationService.findDebtInfoById(cr.getDebtNo());
		debt.setClassifyResult(confirmResult);
		debt.setClassifyDate(CalendarUtil.getCurrentDateStr());
		
		if(cr.getFirstResult().equals("030") || cr.getFirstResult().equals("040") || cr.getFirstResult().equals("050")){
			
			//设置客户违约标志
			CustInfo cust = new CustInfo();
			cust = customerService.findCustByCustId(debt.getCustId());
			cust.setDefaultStat("1");
			customerService.updateCustInfo(cust);
		}
		
		obligationService.updateClassifyRecord(cr);
		obligationService.updateDebtInfo(debt);
		
		response.getWriter().print("{success:true}");
	}
	
	//认定结果提交
	@RequestMapping("/classifyConfirm")
	public void classifyConfirm(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String serialNo = request.getParameter("serialNo");
		
		ClassifyRecord cr = new ClassifyRecord();
		cr = obligationService.findClassifyRecordById(serialNo);
		
		DebtInfo debt = new DebtInfo();
		debt = obligationService.findDebtInfoById(cr.getDebtNo());
		
		
			if(cr.getFirstResult().equals("030") || cr.getFirstResult().equals("040") || cr.getFirstResult().equals("050")){
				
				//设置客户违约标志
				CustInfo cust = new CustInfo();
				cust = customerService.findCustByCustId(debt.getCustId());
				cust.setDefaultStat("1");
				customerService.updateCustInfo(cust);
			}
			if(cr.getConfirmResult() == null || (cr.getConfirmResult()!=null && cr.getConfirmResult().equals(""))){
				cr.setConfirmResult(cr.getFirstResult());
			}
			UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
			cr.setConfirmUserId(user.getUserId());
			cr.setConfirmOrgId(user.getBelongorg());
			cr.setStatus("04");
			cr.setConfirmDate(CalendarUtil.getCurrentDateStr());
			
			debt.setClassifyResult(cr.getConfirmResult());
			debt.setClassifyDate(CalendarUtil.getCurrentDateStr());
		
		
		obligationService.updateClassifyRecord(cr);
		obligationService.updateDebtInfo(debt);
		
		response.getWriter().print("{success:true}");
	}
	
	//打回初分人
	@RequestMapping("/backToFRUser")
	public void backToFRUser(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String serialNo = request.getParameter("serialNo");
		
		ClassifyRecord cr = new ClassifyRecord();
		cr = obligationService.findClassifyRecordById(serialNo);
		
		cr.setStatus("03");
		
		obligationService.updateClassifyRecord(cr);
		
		response.getWriter().print("{success:true}");
	}
	
		//违约事件认定-已认定列表
		@RequestMapping("/listCustDefaultEventConfirmed")
		public void listCustDefaultEventConfirmed(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 List<CustDefaultEvent> custDefaultEventConfirmedList = new ArrayList<CustDefaultEvent>();
			 UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
			 String userId = user.getUserId();
			 String orgId = user.getBelongorg();
			 custDefaultEventConfirmedList = obligationService.findAllCustDefaultEventByAuthID("04",userId,orgId); 
			 
			 List resultList = new ArrayList();
			 
			 for(CustDefaultEvent de: custDefaultEventConfirmedList ){
				 CustInfo cust = new CustInfo();
				 System.out.println(de.getCustId());
				 cust = customerService.findCustByCustId(de.getCustId());
				 if(cust == null) continue;//测试数据不完整，所以需添加此句代码
				 
				 de.setEventType(DataDictionary.getInstance().getNameByCodeAndId("EventType", de.getEventType()));
				 Map map = new HashMap();
				 map.put("custDE", de);
				 map.put("cust", cust);
				 
				 resultList.add(map);
			 }

			 JSONArray JA=JSONArray.fromObject(resultList);
			 out.print(JA);
		}
		
		
		//违约事件认定-待认定列表
		@RequestMapping("/listCustDefaultEventToConfirm")
		public void listCustDefaultEventToConfirm(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 List<CustDefaultEvent> custDefaultEventConfirmedList = new ArrayList<CustDefaultEvent>();
			 UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
			 String userId = user.getUserId();
			 String orgId = user.getBelongorg();
			 custDefaultEventConfirmedList = obligationService.findAllCustDefaultEventByAuthID("02",userId,orgId); 
			 
			 List resultList = new ArrayList();
			 
			 for(CustDefaultEvent de: custDefaultEventConfirmedList ){
				 CustInfo cust = new CustInfo();
				 
				 cust = customerService.findCustByCustId(de.getCustId());
				 if(cust == null) continue;//测试数据不完整，所以需添加此句代码
				 de.setEventType(DataDictionary.getInstance().getNameByCodeAndId("EventType", de.getEventType()));
				 Map map = new HashMap();
				 map.put("custDE", de);
				 map.put("cust", cust);
				 
				 resultList.add(map);
			 }

			 JSONArray JA=JSONArray.fromObject(resultList);
			 out.print(JA);
		}
		
		//显示该客户的所有违约事件
		@RequestMapping("/showCustDE")
		public String showCustDE(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			String custId = request.getParameter("custId");

			if(custId!=null){
				request.getSession().removeAttribute("DECustId");
				request.getSession().setAttribute("DECustId", custId);
						
				CustInfo currentCust = new CustInfo();
				currentCust = customerService.findCustByCustId(custId);
				
				request.getSession().removeAttribute("custId");
				request.getSession().setAttribute("custId", custId);
				request.getSession().removeAttribute("custInfo");
				request.getSession().setAttribute("custInfo", currentCust);
				
				request.getSession().removeAttribute("DECustName");
				request.getSession().setAttribute("DECustName", currentCust.getCustName());
				request.getSession().removeAttribute("DECustType");
				request.getSession().setAttribute("DECustType", DataDictionary.getInstance().getNameByCodeAndId("CustType", currentCust.getCustType()));
			}
			
			return "obligation/CustDefaultEvent_Register";
		}
		
		//列出当前客户的违约事件
		@RequestMapping("/listCustomerDefaultEvent")
		public void listCustomerDefaultEvent(HttpServletRequest request,
				HttpServletResponse response, Model model) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 List<CustDefaultEvent> custDefaultEventList = new ArrayList<CustDefaultEvent>();
			 
			 String custId = request.getSession().getAttribute("DECustId").toString();
			 
			 
			 custDefaultEventList = obligationService.findAllCustDefaultEventByCustId(custId); 
			
			 for(CustDefaultEvent de: custDefaultEventList){
				 de.setEventType(DataDictionary.getInstance().getNameByCodeAndId("EventType", de.getEventType()));
				 de.setStatus(DataDictionary.getInstance().getNameByCodeAndId("DEStatus", de.getStatus()));
			 }

			 JSONArray JA=JSONArray.fromObject(custDefaultEventList);
			 log.info("JSON DATA ："+JA);
			 out.print(JA);
		}
		
		//列出当前客户的违约事件
		@RequestMapping("/findNewestCDE")
		public void findNewestCDE(HttpServletRequest request,
				HttpServletResponse response, Model model) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 List<CustDefaultEvent> custDefaultEventList = new ArrayList<CustDefaultEvent>();
			 
			 String custId = request.getSession().getAttribute("DECustId").toString();
			 
			 custDefaultEventList = obligationService.findAllCustDefaultEventByCustId(custId); 
			
			 CustDefaultEvent cde = null;
			 if(custDefaultEventList!=null && custDefaultEventList.size()>0){
				 cde = custDefaultEventList.get(0);
			 }

			 JSONObject JA=JSONObject.fromObject(cde);
			 log.info("JSON DATA ："+JA);
			 out.print(JA);
		}
		
		
		@RequestMapping("/getCustomer")
			public void getCustomer(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 CustInfo cust = new CustInfo();
			 String custId = null;
			 
			 if (request.getSession().getAttribute("DECustId") != null
						&& !request.getSession().getAttribute("DECustId").toString().equals(""))
				 	custId = request.getSession().getAttribute("DECustId").toString();
			 
			 
			 cust = customerService.findCustByCustId(custId);
			

			 JSONObject Jo=JSONObject.fromObject(cust);
			 out.print(Jo);
		}
		
		//新增客户违约事件
		@RequestMapping("/addCustDefaultEvent")
		public void addCustDefaultEvent(HttpServletRequest request,
				HttpServletResponse response, CustDefaultEvent custDefaultEvent) throws IOException{
			
			custDefaultEvent.setStatus("01");
			custDefaultEvent.setInputDate(CalendarUtil.getCurrentDateStr());
			
			obligationService.insertCustDefaultEvent(custDefaultEvent);
			response.getWriter().print("{success:true}");
		}
		
		
		@RequestMapping("/getCustDefaultEvent")
		public void getCustDefaultEvent(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 CustDefaultEvent custDefaultEvent = new CustDefaultEvent();
			 
			 String serialNo = request.getParameter("serialNo");
			 String custId = request.getSession().getAttribute("DECustId").toString();
			 
			 custDefaultEvent = obligationService.findCustDefaultEventById(serialNo);
			 CustInfo custInfo = customerService.findCustByCustId(custId);
			 
			 Map map = new HashMap();
			 map.put("custDE", custDefaultEvent);
			 map.put("cust", custInfo);

			 JSONObject JA=JSONObject.fromObject(map);
			 out.print(JA);
		}
		
		
			@RequestMapping("/deleteCustDefaultEvent")
			public void deleteCustDefaultEvent(HttpServletRequest request,
					HttpServletResponse response) throws IOException{
				
				CustDefaultEvent custDefaultEvent = new CustDefaultEvent();
				 
				 String serialNo = request.getParameter("serialNo");
				 
				 custDefaultEvent = obligationService.findCustDefaultEventById(serialNo); 
				
				if(custDefaultEvent!=null){
					obligationService.deleteCustDefaultEventById(serialNo);
					response.getWriter().print("{success:true}");
				}else{
					response.getWriter().print("{success:false}");
				}
			}
			
				//编辑当前客户违约事件
				@RequestMapping("/editCustDefaultEvent")
				public void editCustDefaultEvent(HttpServletRequest request,
						HttpServletResponse response, CustDefaultEvent custDefaultEvent) throws IOException{
					
//					CustDefaultEvent custDefaultEvent = new CustDefaultEvent();
//					 
//					String serialNo = request.getParameter("serialNo");
//					
//					String eventType = request.getParameter("eventType");
//					String eventDesc = request.getParameter("eventDesc");
//					String eventDate = request.getParameter("eventDate");
//					
//					custDefaultEvent = obligationService.findCustDefaultEventById(serialNo); 
//					
//					custDefaultEvent.setEventType(eventType);
//					custDefaultEvent.setEventDesc(eventDesc);
//					custDefaultEvent.setEventDate(eventDate);
					
					custDefaultEvent.setStatus("01");
					custDefaultEvent.setInputDate(CalendarUtil.getCurrentDateStr());
					
					obligationService.updateCustDefaultEvent(custDefaultEvent);
					response.getWriter().print("{success:true}");
				}
				
				
				@RequestMapping("/commitCustDefaultEvent")
				public void commitCustDefaultEvent(HttpServletRequest request,
						HttpServletResponse response) throws IOException{
					
					CustDefaultEvent custDefaultEvent = new CustDefaultEvent();
					 
					String serialNo = request.getParameter("serialNo");
					
					custDefaultEvent = obligationService.findCustDefaultEventById(serialNo); 
					
					custDefaultEvent.setStatus("02");
					
					
					obligationService.updateCustDefaultEvent(custDefaultEvent);
					response.getWriter().print("{success:true}");
				}

		//退回登记人
		@RequestMapping("/backToInputUser")
		public void backToInputUser(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			String serialNo = request.getParameter("serialNo");
			
			CustDefaultEvent de = new CustDefaultEvent();
			de = obligationService.findCustDefaultEventById(serialNo);
			
			de.setStatus("03");
			
			obligationService.updateCustDefaultEvent(de);
			
			response.getWriter().print("{success:true}");
		}
		
		
		//违约事件认定
		@RequestMapping("/deConfirm")
		public void deConfirm(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			String serialNo = request.getParameter("serialNo");
			
			CustDefaultEvent de = new CustDefaultEvent();
			de = obligationService.findCustDefaultEventById(serialNo);
			
			de.setStatus("04");
			de.setConfirmDate(CalendarUtil.getCurrentDateStr());
			
			obligationService.updateCustDefaultEvent(de);
			CustInfo custInfo = customerService.findCustByCustId(de.getCustId());
			custInfo.setDefaultStat("1");
			customerService.updateCustInfo(custInfo);
			
			response.getWriter().print("{success:true}");
		}
		
		//查看详情
		@RequestMapping("/getCustDE")
		public void getCustDE(HttpServletRequest request,
				HttpServletResponse response) throws IOException{
			
			 response.setContentType("application/json; charset=utf-8");  
			 response.setHeader("Cache-Control", "no-cache");
			 PrintWriter out = response.getWriter();

			 String serialNo = request.getParameter("serialNo");
				
			CustDefaultEvent de = new CustDefaultEvent();
			de = obligationService.findCustDefaultEventById(serialNo);
			 
			 JSONObject JO=JSONObject.fromObject(de);
			 out.print(JO);
		}
	
	//显示该客户的所有债项
	@RequestMapping("/showDebtList")
	public String showDebt(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		String custId = request.getParameter("CustId");
		
		//response.getWriter().print("{success:true}");

		if(custId!=null){
			request.getSession().removeAttribute("DebtCustId");
			request.getSession().setAttribute("DebtCustId", custId);
			
			request.getSession().removeAttribute("custId");
			request.getSession().setAttribute("custId", custId);
			
			CustInfo currentCust = new CustInfo();
			currentCust = customerService.findCustByCustId(custId);
			
			request.getSession().removeAttribute("custInfo");
			request.getSession().setAttribute("custInfo", currentCust);
			
			request.getSession().removeAttribute("DebtCustName");
			request.getSession().setAttribute("DebtCustName", currentCust.getCustName());
			request.getSession().removeAttribute("DebtCustType");
			request.getSession().setAttribute("DebtCustType", DataDictionary.getInstance().getNameByCodeAndId("CustType", currentCust.getCustType()));
		}
			//response.getWriter().print("{success:true}");
		
		//如果是分类认定中的客户列表
		if(request.getParameter("isConfirm")!=null)
			return "obligation/ClassifyRecordList_DebtList";
		//如果是债项管理的客户列表
		else
			return "obligation/DebtManagement";
	}
	
	
	//显示该客户的所有债项
	@RequestMapping("/viewDebtDetail")
	public String viewDebtDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		String debtNo = request.getParameter("debtNo");	
		//response.getWriter().print("{success:true}");
		request.getSession().removeAttribute("debtNo");
		request.getSession().setAttribute("debtNo", debtNo);
		request.getSession().removeAttribute("debtId");
		request.getSession().setAttribute("debtId", obligationService.findDebtInfoById(debtNo).getDebtId());
		
		DebtInfo debt = new DebtInfo();
		debt = obligationService.findDebtInfoById(debtNo);
		
		request.getSession().removeAttribute("debtDesc");
		request.getSession().setAttribute("debtDesc", debt.getDebtDesc());

		return "obligation/DebtInfoDetail";
	}
	
	//新增债项
	@RequestMapping("/addDebtDetail")
	public String addDebtDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		request.getSession().removeAttribute("debtNo");
		request.getSession().removeAttribute("debtId");
		request.getSession().removeAttribute("debtDesc");
		
		return "obligation/AddDebtInfoDetail";
	}
	
	
	//获取债项详情
	@RequestMapping("/getDebtInfo")
	public void getDebtInfo(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String debtNo = "";
		if(request.getSession().getAttribute("debtNo")!=null)
			debtNo =  request.getSession().getAttribute("debtNo").toString();
		DebtInfo debtInfo = null;
		Map map = new HashMap();
		if(debtNo!=null){
			debtInfo = obligationService.findDebtInfoById(debtNo);
			String debtTypeLevelOne = Debt.getInstance().getDebtTypeLevelOneCodeByLevelTwoCode(debtInfo.getDebtType());//作废的方式：debtInfo.getDebtType().substring(0, 2);
			debtInfo.setDebtTypeLevelOne(debtTypeLevelOne);//手工设置大类code，保证显示
		}
			
		if(debtInfo!= null)
			map.put("success", true);
		else
			map.put("failure", false);
		map.put("data", debtInfo);
		
		JSONObject debtJO = JSONObject.fromObject(map);
		
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(debtJO);
	}
	
	
	
	//显示该客户的所有债项
	@RequestMapping("/listDebt")
	public void listDebt(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		
		response.setContentType("application/json; charset=utf-8");  
	    response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		 
		String debtCustId = request.getSession().getAttribute("DebtCustId").toString();
		List<DebtInfo> debtList = new ArrayList<DebtInfo>(); 
		

		List resultList = new ArrayList();
		

		debtList = obligationService.findDebtByCustId(debtCustId); 
		
		for(DebtInfo di : debtList ){
			//TODO 债项业务品种无
			di.setDebtType(DataDictionary.getInstance().getNameByCodeAndId("DebtType", di.getDebtType()));
			di.setCurrency(DataDictionary.getInstance().getNameByCodeAndId("Currency", di.getCurrency()));
			//di.setBusinessKind(DataDictionary.getInstance().getNameByCodeAndId("BusinessKind", di.getBusinessKind()));
			di.setGuarantyKind(DataDictionary.getInstance().getNameByCodeAndId("GuarantyKind", di.getGuarantyKind()));
			di.setClassifyResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", di.getClassifyResult()));
			
		}
		
		JSONArray JA=JSONArray.fromObject(debtList);
		out.print(JA);
	}
	
	//新增债项t
	@RequestMapping("/addDebt")
	public void addDebt(HttpServletRequest request,
			HttpServletResponse response,DebtInfo debtInfo) throws IOException{
		String debtCustId = request.getSession().getAttribute("DebtCustId").toString();
		debtInfo.setCustId(debtCustId);
		debtInfo.setCustName(customerService.getCustNameById(debtCustId));
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		debtInfo.setInputUserId(user.getUserId());
		debtInfo.setInputOrgId(user.getBelongorg());
		debtInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		debtInfo.setUpdateUserId(user.getUserId());
		debtInfo.setUpdateOrgId(user.getBelongorg());
		debtInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		
		obligationService.insertDebt(debtInfo);
		customerService.updateBalance(debtCustId);
		request.getSession().removeAttribute("debtNo");
		request.getSession().setAttribute("debtNo", debtInfo.getDebtNo());
		request.getSession().removeAttribute("debtId");
		request.getSession().setAttribute("debtId", debtInfo.getDebtId());
		
		request.getSession().removeAttribute("debtDesc");
		request.getSession().setAttribute("debtDesc", debtInfo.getDebtDesc());
		
		response.getWriter().print("{success:true}");
	}
	
	//获取单一债项
	@RequestMapping("/getDebt")
	public void getDebt(HttpServletRequest request,
			HttpServletResponse response) throws IOException{

		String debtNo =  request.getParameter("debtNo");
		DebtInfo debtInfo= new DebtInfo();
		debtInfo = obligationService.findDebtInfoById(debtNo);
		JSONObject debtJO = JSONObject.fromObject(debtInfo);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(debtJO);
	}
	
	//编辑债项
	@RequestMapping("/editDebt")
	public void editDebt(HttpServletRequest request,
			HttpServletResponse response,DebtInfo debtInfo) throws IOException {
		
		String debtNo = request.getParameter("debtNo");
		DebtInfo temp = new DebtInfo();
		//编辑信用信息
		if(debtNo==null || debtNo.equals("")){
			debtNo = request.getSession().getAttribute("debtNo").toString();
			temp = obligationService.findDebtInfoById(debtNo);
			temp.setDefaultBalance(Double.valueOf(request.getParameter("defaultBalance")));
			temp.setDefaultDate(request.getParameter("defaultDate"));
			temp.setDefaultInterestSum(Double.valueOf(request.getParameter("defaultInterestSum")));
			temp.setDefaultReason(request.getParameter("defaultReason"));
			temp.setClassifyResult(request.getParameter("classifyResult"));
			temp.setClassifyDate(request.getParameter("classifyDate"));
			obligationService.updateDebtInfo(temp);
			customerService.updateBalance(temp.getCustId());
		}
		//编辑基本信息
		else{
			temp = obligationService.findDebtInfoById(debtNo);
			debtInfo.setClassifyDate(temp.getClassifyDate());
			debtInfo.setClassifyResult(temp.getClassifyResult());
			debtInfo.setDefaultBalance(temp.getDefaultBalance());
			debtInfo.setDefaultDate(temp.getDefaultDate());
			debtInfo.setDefaultReason(temp.getDefaultReason());
			debtInfo.setDefaultInterestSum(temp.getDefaultInterestSum());
			debtInfo.setInputDate(temp.getInputDate());
			debtInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
			obligationService.updateDebtInfo(debtInfo);
			customerService.updateBalance(temp.getCustId());
		}
		
		
		response.getWriter().print("{success:true}");
	}
	//检查是否可以产出债项
	@RequestMapping("/checkDeleteDebt")
	public void checkDeleteDebt(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		String debtNo = request.getParameter("debtNo"); 
		if(obligationService.hasUsed(debtNo))
			response.getWriter().print("{success:false}");
		else
			response.getWriter().print("{success:true}");
	}
	
	//删除债项
	@RequestMapping("/deleteDebt")
	public void deleteDebt(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		String debtNo = request.getParameter("debtNo"); 
		DebtInfo debt = obligationService.findDebtInfoById(debtNo);
		
		if(debt!=null){
			customerService.updateBalance(debt.getCustId());
			obligationService.deleteDebtById(debtNo);
			response.getWriter().print("{success:true}");
		}
		else{
			response.getWriter().print("{success:false}");
		}
	}
	
	//获取债项所有资产分类
	@RequestMapping("/listDebtAllClassifyRecord")
	public void listDebtAllClassifyRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 List<ClassifyRecord> debtClassifyRecordList = new ArrayList<ClassifyRecord>();
		 
		 String debtNo = request.getParameter("debtNo");

		 debtClassifyRecordList = obligationService.findClassifyRecordByDebtNo(debtNo);
		
		 for(ClassifyRecord cr: debtClassifyRecordList){
			 
			 cr.setConfirmResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getConfirmResult()));
			 cr.setFirstResult(DataDictionary.getInstance().getNameByCodeAndId("ClassifyResult", cr.getFirstResult()));
			 cr.setStatus(DataDictionary.getInstance().getNameByCodeAndId("ClassifyStatus", cr.getStatus()));
			 
		 }

		 JSONArray JA=JSONArray.fromObject(debtClassifyRecordList);
		 log.info("JSON　DATA: "+JA);
		 out.print(JA);
	}

	//获取最新资产分类
	@RequestMapping("/findNewestCR")
	public void findNewestCR(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		 response.setContentType("application/json; charset=utf-8");  
		 response.setHeader("Cache-Control", "no-cache");
		 PrintWriter out = response.getWriter();

		 List<ClassifyRecord> debtClassifyRecordList = new ArrayList<ClassifyRecord>();
		 
		 String debtNo = request.getParameter("serialNo");

		 debtClassifyRecordList = obligationService.findClassifyRecordByDebtNo(debtNo);
		
		 ClassifyRecord cr = null;
		 if(debtClassifyRecordList!=null && debtClassifyRecordList.size()>0){
			 cr = debtClassifyRecordList.get(0);
		 }

		 JSONObject JA=JSONObject.fromObject(cr);
		 log.info("JSON　DATA: "+JA);
		 out.print(JA);
	}	
	
	
	//新增信贷资产分类
	
	@RequestMapping("/addClassifyRecord")
	public void addClassifyRecord(HttpServletRequest request,
			HttpServletResponse response,ClassifyRecord classifyRecord) throws IOException{
		
		classifyRecord.setFirstDate(CalendarUtil.getCurrentDateStr());
		classifyRecord.setStatus("01");
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		classifyRecord.setFruserId(user.getUserId());
		classifyRecord.setFrorgId(user.getBelongorg());
		
		obligationService.insertClassifyRecord(classifyRecord);
		response.getWriter().print("{success:true}");
	}
	
	
	//获取单一资产分类结果
	@RequestMapping("/getClassifyRecord")
	public void getClassifyRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException{

		String serialNo =  request.getParameter("serialNo");
		ClassifyRecord classifyRecord = new ClassifyRecord();
		classifyRecord = obligationService.findClassifyRecordById(serialNo);
		
		Map map = new HashMap();
		map.put("success", true);
		map.put("data", classifyRecord);
		
		JSONObject JO = JSONObject.fromObject(map);
		
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(JO);
	}

	//编辑信贷资产分类
	
	@RequestMapping("/editClassifyRecord")
	public void editClassifyRecord(HttpServletRequest request,
			HttpServletResponse response, ClassifyRecord classifyRecord) throws IOException{
		
		classifyRecord.setFirstDate(CalendarUtil.getCurrentDateStr());
		classifyRecord.setStatus("01");
		
		obligationService.updateClassifyRecord(classifyRecord);
		response.getWriter().print("{success:true}");
	}
	
	
	@RequestMapping("/commitClassifyRecord")
	public void commitClassifyRecord(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		ClassifyRecord classifyRecord = new ClassifyRecord();
		
		String serialNo = request.getParameter("serialNo");
		
		classifyRecord = obligationService.findClassifyRecordById(serialNo);
		classifyRecord.setStatus("02");
		
		obligationService.updateClassifyRecord(classifyRecord);
		response.getWriter().print("{success:true}");
	}
	//显示当期债项的所有抵质押品信息
	@RequestMapping("/listDebtCollateral")
	public void listDebtCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		response.setContentType("application/json; charset=utf-8");  
	    response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		 
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		//String debtCustId = request.getSession().getAttribute("DebtCustId").toString();
		List<DebtCollateral> debtCollateralList = obligationService.findDebtCollateralByDebtNo(debtNo); 
		
		List resultList = new ArrayList();
		
		for(DebtCollateral dc : debtCollateralList ){
			
			Map map = new HashMap();
			DebtInfo debt = new DebtInfo();
			CollateralInfo collateral = new CollateralInfo();
			debt = obligationService.findDebtInfoById(dc.getId().getDebtNo());
			collateral = obligationService.findCollateralById(dc.getId().getCollateralId());
			collateral.setCollateralType(DataDictionary.getInstance().getNameByCodeAndId("GuarantyType", collateral.getCollateralType()));
			
			map.put("debt", debt);
			map.put("collateral", collateral);
			resultList.add(map);
		}
		
		JSONArray JA=JSONArray.fromObject(resultList);
		out.print(JA);
	}
	
	//为当前债项新增抵质押品
	@RequestMapping("/addDebtCollateral")
	public void addDebtCollateral(HttpServletRequest request,
			HttpServletResponse response,DebtCollateral debtCollateral) throws IOException{
		
		String collateralId = request.getParameter("collateralId");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		
		DebtCollateralId id = new DebtCollateralId();
		id.setCollateralId(collateralId);
		id.setDebtNo(debtNo);
		debtCollateral.setId(id);
		
		obligationService.insertDebtCollateral(debtCollateral);
		response.getWriter().print("{success:true}");
	}
	
	//删除当前债项的一笔抵质押品
	@RequestMapping("/deleteDebtCollateral")
	public void deleteDebtCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		DebtCollateral debtCollateral = new DebtCollateral();
		
		String collateralId = request.getParameter("collateralId");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		
		DebtCollateralId id = new DebtCollateralId();
		id.setCollateralId(collateralId);
		id.setDebtNo(debtNo);
		debtCollateral = obligationService.findDebtCollateralById(id);
		if(debtCollateral != null){
			obligationService.deleteDebtCollateral(debtCollateral);
			response.getWriter().print("{success:true}");
		}
		
	}
	
	//编辑当前债项的一笔抵质押品
	@RequestMapping("/editDebtCollateral")
	public void editDebtCollateral(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		String collateralId = request.getParameter("collateralId");
		String shareString = request.getParameter("share");
		Double share = 0.0;
		if(shareString != null || !"".equals(shareString))
			share = Double.parseDouble(shareString);
		
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		
		DebtCollateral debtCollateral = new DebtCollateral();
		
		DebtCollateralId id = new DebtCollateralId();
		id.setCollateralId(collateralId);
		id.setDebtNo(debtNo);
		
		debtCollateral = obligationService.findDebtCollateralById(id);
		
		if(debtCollateral != null){
			debtCollateral.setShare(share);
			obligationService.updateDebtCollateral(debtCollateral);
			response.getWriter().print("{success:true}");
		}
		
	}
	
	//显示当期债项的所有的保证信息
	@RequestMapping("/listDebtGuarantor")
	public void listDebtGuarantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		
		response.setContentType("application/json; charset=utf-8");  
	    response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		 
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		List<GuaranteeInfo> debtGuarantorList = obligationService.findDebtGuarantorByDebtNo(debtNo); 
		
		for(GuaranteeInfo gi : debtGuarantorList ){
			//将id变成name
			gi.setDebtName(obligationService.findDebtInfoById(debtNo).getDebtDesc());
			gi.setGuarantyType(DataDictionary.getInstance().getNameByCodeAndId("GuarantyType",gi.getGuarantyType()));
			gi.setGuarantorRelative(DataDictionary.getInstance().getNameByCodeAndId("GuarantyRelativeType",gi.getGuarantorRelative()));
			gi.setGuarantorScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope",gi.getGuarantorScope()));
			gi.setGuarantorName(obligationService.findGuarantorById(gi.getGid().getGuarantorId()).getCustName());
		}
	
		JSONArray JA=JSONArray.fromObject(debtGuarantorList);
		out.print(JA);
	}
	
	
	//新增债项保证信息
	@RequestMapping("/addDebtGuarantee")
	public void addDebtGuarantee(HttpServletRequest request,
			HttpServletResponse response,GuaranteeInfo guaranteeInfo) throws IOException{
		String guarantorId =  request.getParameter("guarantorId");
		String guarantyTypeOne = request.getParameter("guarantyTypeLevelOne");
		String guarantyTypeTwo = request.getParameter("guarantyTypeLevelTwo");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		GuaranteeInfoId id= new GuaranteeInfoId();
		id.setDebtNo(debtNo);
		id.setGuarantorId(guarantorId);
		guaranteeInfo.setGid(id);
		
		if(guarantorId!=null){
			CustInfo custInfo = customerService.findCustByCustId(guarantorId);
			if(custInfo !=null && !custInfo.getCustName().equals("")){
				guaranteeInfo.setGuarantorName(custInfo.getCustName());
			}
		}
		guaranteeInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		guaranteeInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		guaranteeInfo.setGuarantyType(request.getParameter("guarantyTypeLevelTwo"));
		
		obligationService.insertDebtGuarantee(guaranteeInfo);
		response.getWriter().print("{success:true}");
	}
	
	//获取单一保证信息
	@RequestMapping("/getDebtGuarantee")
	public void getDebtGuarantee(HttpServletRequest request,
			HttpServletResponse response) throws IOException{

		String guarantorId =  request.getParameter("guarantorId");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		
		GuaranteeInfoId id= new GuaranteeInfoId();
		id.setDebtNo(debtNo);
		id.setGuarantorId(guarantorId);
		GuaranteeInfo guaranteeInfo = obligationService.findDebtGuarantorById(id);
		
		Map<String, String> guaranty = Guaranty.getInstance().getGuaranty();
		
		if(guaranteeInfo.getGuarantyType() != null && !guaranteeInfo.getGuarantyType().equals("")){
			guaranteeInfo.setGuarantyTypeLevelOne(guaranteeInfo
					.getGuarantyType().substring(0, 2));
			guaranteeInfo.setGuarantyTypeLevelTwo(guaranteeInfo
					.getGuarantyType());
		}
		
		
		JSONObject debtJO = JSONObject.fromObject(guaranteeInfo);
		response.setContentType("application/json; charset=utf-8");  
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print(debtJO);
	}
	

	//编辑债项
	@RequestMapping("/editDebtGuarantee")
	public void editDebtGuarantee(HttpServletRequest request,
			HttpServletResponse response,GuaranteeInfo guaranteeInfo) throws IOException {
		String guarantorId =  request.getParameter("guarantorId");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		GuarantorInfo gi = obligationService.findGuarantorById(guarantorId);
				
		GuaranteeInfoId id= new GuaranteeInfoId();
		id.setDebtNo(debtNo);
		id.setGuarantorId(guarantorId);
		guaranteeInfo.setGid(id);
		guaranteeInfo.setGuarantorName(gi.getCustName());
		guaranteeInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		guaranteeInfo.setGuarantyType(request.getParameter("guarantyTypeLevelTwo"));
		
		obligationService.updateDebtGuaranteeInfo(guaranteeInfo);
		response.getWriter().print("{success:true}");
	}
	
	//添加债项信用信息
	@RequestMapping("/addDebtCreditInfo")
	public void addDebtCreditInfo(HttpServletRequest request,
			HttpServletResponse response, Model model){
		String defaultDate = request.getParameter("defaultDate");
		String defaultBalance =  request.getParameter("defaultBalance");
		String defaultInterestSum = request.getParameter("defaultInterestSum");
		String defaultReason = request.getParameter("defaultReason");
		
		
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		DebtInfo debt = obligationService.findDebtInfoById(debtNo);
		
		if(debt!=null){
			debt.setDefaultDate(defaultDate);
			debt.setDefaultBalance(Double.valueOf(defaultBalance));
			debt.setDefaultInterestSum(Double.valueOf(defaultInterestSum));
			debt.setDefaultReason(defaultReason);
		}
		
		if(debt!=null){
			obligationService.updateDebtInfo(debt);
			model.addAttribute("debt", debt);
		}
		
	}
	
	/*
	 * 添加客户 添加保证人信息
	 */
	@RequestMapping("/addGuarantor")
	public void addGuarantor(HttpServletRequest request,
			HttpServletResponse response, GuarantorInfo guarantorInfo, Model model){
		UserInfo user = (UserInfo)request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		// 设置当前时间
		guarantorInfo.setInputDate(CalendarUtil.getCurrentDateStr());
		guarantorInfo.setInputUserId(userId);
		guarantorInfo.setUpdateDate(CalendarUtil.getCurrentDateStr());
		
		obligationService.insertGuarantorInfo(guarantorInfo);
		
		Map<String, Object> myCustMap = new HashMap<String, Object>(2);
		myCustMap.put("success", "true");
		myCustMap.put("guarantorInfo", guarantorInfo);
		JSONObject myCustJO=JSONObject.fromObject(myCustMap);
		
		log.info("JSON data: "+myCustJO);
		
	}
	
	//删除保证信息
	@RequestMapping("/deleteDebtGurantor")
		public void deleteDebtGurantor(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String guarantorId = request.getParameter("guarantorId");
		String debtNo = request.getSession().getAttribute("debtNo").toString();
		GuaranteeInfoId id = new GuaranteeInfoId();
		id.setGuarantorId(guarantorId);
		id.setDebtNo(debtNo);
		GuaranteeInfo guaranteeInfo = obligationService.findDebtGuarantorById(id);
		
		
		if(guaranteeInfo!=null){
			obligationService.deleteDebtGuarantee(guaranteeInfo);
			response.getWriter().print("{success:true}");
		}
		else{
			response.getWriter().print("{success:false}");
		}
	}
	
	
}
